package org.example.hotel.service;

import org.example.hotel.dto.CheckinRequest;
import org.example.hotel.entity.CheckinRecord;
import org.example.hotel.entity.Room;
import org.example.hotel.repository.CheckinRecordRepository;
import org.example.hotel.repository.RoomRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CheckinService {

    @Autowired
    private RoomRepository roomRepository;

    @Autowired
    private CheckinRecordRepository checkinRecordRepository;

    public void checkin(CheckinRequest request) {
        Room room = roomRepository.findById(request.getRoomId()).orElseThrow();

        if (room.isOccupied()) {
            throw new IllegalStateException("房间已被占用！");
        }

        room.setOccupied(true);
        room.setCheckInTime(LocalDateTime.now());
        room.setTargetTemp(25.0); // 缺省温度
        room.setCurrentTemp(room.getInitialTemp());
        room.setFanSpeed("MEDIUM");
        room.setAcOn(false);
        room.setServing(false);
        room.setWorkMode("COLD");
        roomRepository.save(room);

        CheckinRecord record = new CheckinRecord();
        record.setRoomId(request.getRoomId());
        record.setCustomerName(request.getCustomerName());
        record.setCheckinTime(LocalDateTime.now());
        record.setRoomCost(200.0);
        checkinRecordRepository.save(record);
    }
}

